//
//  SliderToolbar.m
//  播放器模拟(列表)
//
//  Created by lanou3g on 15/11/2.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "SliderToolbar.h"


@interface SliderToolbar ()



// 防止多次创建
@property (nonatomic, copy) NSString *totalTimeText;
@property (nonatomic, copy) NSString *currentTimeText;

@end

@implementation SliderToolbar





- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.totalTimeLabel.frame = CGRectMake(300, 30, 40, 20);
    self.totalTimeLabel.font = [UIFont systemFontOfSize:13];
    
    self.currentTimeLabel.frame = CGRectMake(60, 30, 40, 20);
    self.currentTimeLabel.font = [UIFont systemFontOfSize:13];
    
    self.slider.frame = CGRectMake(70, 10, 250, 20);
    
    // 播放按钮
    self.playButton.frame = CGRectMake(10, 10, 32, 32);
}

// 将秒数转换为字符串
- (NSString *)convertSecondToMinute:(CGFloat)seconds
{
    int minute = seconds / 60;
    int second = (int)seconds % 60;
    
    NSString *timeLabel = [NSString stringWithFormat:@"%02d:%02d", minute, second];
    return timeLabel;
}

#pragma mark  - 重写set方法
- (void)setTotalTime:(CGFloat)totalTime
{
    _totalTime = totalTime;
    self.totalTimeText = [self convertSecondToMinute:totalTime];
    
    self.totalTimeLabel.text = self.totalTimeText;
}

- (void)setCurrentTime:(CGFloat)currentTime
{
    _currentTime = currentTime;
    self.currentTimeText = [self convertSecondToMinute:currentTime];
    
    self.currentTimeLabel.text = self.currentTimeText;
}


/**
 *  play按钮点击方法
 */
- (void)playButtonDidClick:(UIButton *)btn
{
    btn.selected = !btn.isSelected;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(sliderToolbar:playButtonDidClick:)]) {
        [self.delegate sliderToolbar:self playButtonDidClick:btn];
    }
    
}

#pragma mark - 懒加载
- (UILabel *)totalTimeLabel
{
    if (_totalTimeLabel == nil) {
        _totalTimeLabel = [[UILabel alloc] init];
        
        
        [self addSubview:_totalTimeLabel];
    }
    return _totalTimeLabel;
}

- (UILabel *)currentTimeLabel
{
    if (_currentTimeLabel == nil) {
        _currentTimeLabel = [[UILabel alloc] init];
        [self addSubview:_currentTimeLabel];
    }
    return _currentTimeLabel;
}

- (Slider *)slider
{
    if (_slider == nil) {
        _slider = [[Slider alloc] init];
        [self addSubview:_slider];
    }
    return _slider;
}

- (UIButton *)playButton
{
    if (_playButton == nil) {
        _playButton = [[UIButton alloc] init];
        [_playButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        [_playButton setImage:[UIImage imageNamed:@"play2"] forState:UIControlStateSelected];
        [_playButton addTarget:self action:@selector(playButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_playButton];
    }
    return _playButton;
}



@end
